<template>
  <div class="login-container">
<!--    <el-button type="primary" plain @click="getToken">获取Token</el-button>-->
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
        <div class="title-tips">欢迎来到教师管理考评系统</div>
        <div id="login_container"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {getToken} from '@/api/login';
  export default {
    name: 'Login',
    data() {
      return {
        //appid
        //appid:'dingoa70d0ek6fb9vq5lsr',
        //frontEndRedirectUri:'http://www.dudian.group/TeacherEvaluation/',
        //getUserInfoRedirectUri:'http://www.dudian.group/TeacherEvaluation/getUserInfo'
        //lizhiwei
        //appid:'dingoak6ojrcv3hr91gjd2',
        //frontEndRedirectUri:'http://qiuhe.free.idcfengye.com/TeacherEvaluation/',
        //getUserInfoRedirectUri:'http://qiuhe.free.idcfengye.com/TeacherEvaluation/getUserInfo'
        //slw
        /*appid:'dingoatikcdslhwirjx1kt',
        frontEndRedirectUri:'http://www.dudian.group/TeacherEvaluation/',
        getUserInfoRedirectUri:'http://www.dudian.group/TeacherEvaluation/getUserInfo'*/


        appid:'dingoatikcdslhwirjx1kt',
        frontEndRedirectUri:'http://120.71.149.155:18080/TeacherEvaluation/',
        getUserInfoRedirectUri:'http://120.71.149.155:18080/TeacherEvaluation/getUserInfo'

      }
    },
    mounted () {
      if (this.$route.query.appid) {
        this.appid = this.$route.query.appid
      }
      // this.appid = this.$route.query.appid
      window.sessionStorage.setItem('appid', this.appid)

      this.codeLogin()
    },
    destroyed() {
    },
    methods: {
        getToken(){
            getToken().then((res) => {
                window.sessionStorage.setItem('token',res.token)
                this.$router.push({ path: this.redirect || '/'})
            }).catch(() => {

            })
        },
      // 获取钉钉扫码相关参数
      codeLogin () {
        this.appid = this.appid
        this.frontEndRedirectUri = this.frontEndRedirectUri
        let obj = DDLogin({
          id: 'login_container',
          // goto这里需要对url整体做一个urlencode编码
          goto: encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=' + this.appid + '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + this.getUserInfoRedirectUri + ''),
          style: 'border:none;background-color:#FFFFFF;',
          width: '365',
          height: '300'
        })
        let hanndleMessage = function (event) {
          let origin = event.origin
          // 判断是否来自ddLogin扫码事件
          if (origin === 'https://login.dingtalk.com') {
            let loginTmpCode = event.data
            // 这里url不用进行urlencode编码
            let url = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=' + this.appid + '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + this.getCodeRedirectUri + '&loginTmpCode=' + loginTmpCode
            window.location.href = url
          }
        }
        if (typeof window.addEventListener !== 'undefined') {
          window.addEventListener('message', hanndleMessage, false)
        } else if (typeof window.attachEvent !== 'undefined') {
          window.attachEvent('onmessage', hanndleMessage)
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100vh;
    background: url("~@/assets/images/background.jpg") center center fixed
    no-repeat;
    background-size: cover;

    .title {
      font-size: 54px;
      font-weight: 500;
      color: rgba(14, 18, 26, 1);
    }

    .title-tips {
      margin-top: 160px;
      font-size: 26px;
      font-weight: 400;
      color: rgba(14, 18, 26, 1);
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .login-btn {
      display: inherit;
      width: 220px;
      height: 60px;
      margin-top: 5px;
      border: 0;

      &:hover {
        opacity: 0.9;
      }
    }

    .login-form {
      position: relative;
      max-width: 100%;
      margin: calc((100vh - 425px) / 2) 10% 10%;
      overflow: hidden;

      .forget-password {
        width: 100%;
        margin-top: 40px;
        text-align: left;

        .forget-pass {
          width: 129px;
          height: 19px;
          font-size: 20px;
          font-weight: 400;
          color: rgba(92, 102, 240, 1);
        }
      }
    }

    .tips {
      margin-bottom: 10px;
      font-size: 14px;
      color: #fff;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .title-container {
      position: relative;

      .title {
        margin: 0 auto 40px auto;
        font-size: 34px;
        font-weight: bold;
        color: #1890ff;
        text-align: center;
      }
    }

    .svg-container {
      position: absolute;
      top: 14px;
      left: 15px;
      z-index: 999;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    .show-password {
      position: absolute;
      top: 14px;
      right: 25px;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    ::v-deep {
      .el-form-item {
        padding-right: 0;
        margin: 20px 0;
        color: #454545;
        background: transparent;
        border: 1px solid transparent;
        border-radius: 2px;

        &__content {
          min-height: 32px;
          line-height: 32px;
        }

        &__error {
          position: absolute;
          top: 100%;
          left: 18px;
          font-size: 12px;
          line-height: 18px;
          color: #ff4d4f;
        }
      }

      .el-input {
        box-sizing: border-box;

        input {
          height: 58px;
          padding-left: 45px;
          font-size: 14px;
          line-height: 58px;
          color: #606266;
          background: #f6f4fc;
          border: 0;
          caret-color: #606266;
        }
      }
    }
  }
</style>
